//
//  ViewController.m
//  FloatingViewByFicow
//
//  Created by ficow on 1/8/16.
//  Copyright © 2016年 ficow. All rights reserved.
//

#import "ViewController.h"
#import "FloatingView.h"

@interface ViewController ()

@property (nonatomic, strong) FloatingView *fv;

@property (nonatomic, strong) UIDynamicAnimator *animator;
@property (nonatomic, strong) UIGravityBehavior *gravity;
@property (nonatomic, strong) UICollisionBehavior *collision;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    _fv = [[FloatingView alloc]initWithFrame:CGRectMake(0, 150, 80, 80)];
    __weak __typeof(self) weakSelf = self;
    _fv.finishMoveBlock = ^(BOOL toLeft){
        __strong __typeof(weakSelf) self = weakSelf;
        
        self.animator = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
        self.gravity = [[UIGravityBehavior alloc]initWithItems:@[self.fv]];
        
        CGVector dir;
        if (toLeft) {
            dir = CGVectorMake(-0.1, 0.0);
        }else{
            dir = CGVectorMake(0.1, 0.0);
        }
        [self.gravity setGravityDirection:dir];
        self.gravity.magnitude = 7;
        [self.animator addBehavior:self.gravity];
        
        self.collision = [[UICollisionBehavior alloc]initWithItems:@[self.fv]];
        self.collision.translatesReferenceBoundsIntoBoundary = YES;
        [self.animator addBehavior:self.collision];
    };
    
    [self.view addSubview:_fv];
    
    
    
    UILabel *lb = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    lb.text = @"A demo showing floating view --- by Ficow Shen";
    [lb sizeToFit];
    lb.center = self.view.center;
    [self.view addSubview:lb];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
